/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rainbow.xml;

import java.io.File;
import java.io.IOException;
import java.util.List;
import org.rainbow.file.FileWriter;
import org.rainbow.udo.UConnection;
import org.rainbow.udo.UdoHandleException;
import org.rainbow.udo.UQuery;

/**
 *
 * @author hironaga
 */
public class XmlWriter extends FileWriter{

    private static XmlNativeHandler nativeHandler = new XmlNativeHandlerImpl();

    protected static XmlCommonHandler commonHandler = XmlCommonHandler.getInstance();

    public XmlWriter(UConnection uconn) {
        super(uconn);
    }

    protected boolean forAppend(File file) throws IOException, UdoHandleException{

        if(file.exists()){
            if(tmpFile == null){
                createTmpFile();
                if(commonHandler.isNewDocument(file)){
                    return false;
                }
                else{
                    return true;
                }
            }
            else{
                return true;
            }
        }
        else{
            if(tmpFile == null){
                createTmpFile();
                return false;
            }
            else if(commonHandler.isNewDocument(tmpFile)){
                return false;
            }
            else{
                return true;
            }
        }
    }

    public void create(UQuery uquery, Object data)
                                                   throws UdoHandleException {

        File file = uconn.getFile();
        //createTmpFile();
        String xpath = uquery.getXpath().getXpath();

        try{
            if(forAppend(file)){
                nativeHandler.appendObject(tmpFile, xpath, data);
            }
            else{
                nativeHandler.writeObject(tmpFile, xpath, data);
            }
        } catch (UdoHandleException ex){
            close();
        } catch (IOException ex) {
            close();
            throw new UdoHandleException(ex);
        }
        
    }

    public void createList(UQuery uquery, List<?> dataList) throws UdoHandleException {

        File file = uconn.getFile();
        //createTmpFile();
        String xpath = uquery.getXpath().getXpath();

        try{
            if(forAppend(file)){
                nativeHandler.appendList(tmpFile, xpath, dataList);
            }
            else{
                nativeHandler.writeList(tmpFile, xpath, dataList);
            }
        } catch (UdoHandleException ex){
            close();
        } catch (IOException ex) {
            close();
            throw new UdoHandleException(ex);
        }
        
    }
    

//    public <T> T read(UQuery uquery, Class<T> clazz)
//                                                   throws UdoHandleException{
//
//        File file = getFile();
//        String xpath = uquery.getXpath().getXpath();
//        try {
//            return  nativeHandler.readObject(file, xpath, clazz);
//        } catch (IOException ex) {
//            close();
//            throw new UdoHandleException(ex);
//        }
//    }
//
//    public <T> List<T> readList(UQuery uquery, Class<T> clazz)
//                                                    throws UdoHandleException {
//
//        File file = getFile();
//        String xpath = uquery.getXpath().getXpath();
//        try {
//            return nativeHandler.readList(file, xpath, clazz);
//        } catch (IOException ex) {
//            close();
//            throw new UdoHandleException(ex);
//        }
//    }

    public void update(UQuery uquery, Object data) throws UdoHandleException {

        createTmpFile();
        String xpath = uquery.getXpath().getXpath();
        try {
            nativeHandler.overwriteObject(tmpFile, xpath, data);
        } catch (IOException ex) {
            close();
            throw new UdoHandleException(ex);
        }

    }

    public void updateList(UQuery uquery, List<?> dataList) throws UdoHandleException {
        //TODO 実装
    }

    public void delete(UQuery uquery) throws UdoHandleException {
        //TODO 実装
    }

}
